package cc.smarnet.caihou.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.Serializable;
import java.util.List;

import cc.smarnet.caihou.R;
import cc.smarnet.caihou.entity.Deliver;
import cc.smarnet.caihou.phone.deliver.DeliverDetailActivity;


/**
* 出库信息的adapter
* */
public class DeliverAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Deliver> list;

    public DeliverAdapter(Context context, List<Deliver> list){
        this.context=context;
        this.list=list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.deliver_item,parent,false);
        deliverViewHolder deliverViewHolder=new deliverViewHolder(view);
        return deliverViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Deliver deliver=list.get(position);
        deliverViewHolder deliverViewHolder= (deliverViewHolder) holder;
        deliverViewHolder.carDerver.setText(deliver.getCarDerver());
        deliverViewHolder.carNo.setText(deliver.getCarNo());
        deliverViewHolder.number.setText(deliver.getNumber());
        deliverViewHolder.orderNo.setText(deliver.getOrderNo());
        deliverViewHolder.state.setText(deliver.getStateSting());
        deliverViewHolder.stockInTime.setText(deliver.getStockInTime());
        deliverViewHolder.stockOutUser.setText(deliver.getStockOutUser());
        deliverViewHolder.remarks.setText(deliver.getRemarks());
    }

    @Override
    public int getItemCount() {
        // list.size()为0的话也必须至少返回1，返回有数值的话才会显示item，因为该方法不会返回0
        return list.size() > 0 ? list.size() : 1;
    }

    class deliverViewHolder extends RecyclerView.ViewHolder{
        TextView carDerver;
        TextView carNo;
        TextView number;
        TextView orderNo;
        TextView state;
        TextView stockInTime;
        TextView stockOutUser;
        TextView remarks;

        public deliverViewHolder(@NonNull View itemView) {
            super(itemView);
            carDerver=itemView.findViewById(R.id.deliver_item_carDerver);
            carNo=itemView.findViewById(R.id.deliver_item_carNo);
            number=itemView.findViewById(R.id.deliver_item_number);
            orderNo=itemView.findViewById(R.id.deliver_item_orderNo);
            state=itemView.findViewById(R.id.deliver_item_state);
            stockInTime=itemView.findViewById(R.id.deliver_item_stockInTime);
            stockOutUser=itemView.findViewById(R.id.deliver_item_stockOutUser);
            remarks=itemView.findViewById(R.id.deliver_item_remarks);

            // item点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Deliver deliver=list.get(getAdapterPosition());
                    Intent intent = new Intent(context, DeliverDetailActivity.class);
                    intent.putExtra("deliverDetail", (Serializable) deliver);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                    context.startActivity(intent);
                }
            });
        }
    }
}
